from typing import re

from fastapi import requests


def kxdaili():
    for i in range(10):#10页
        url = f'http://www.kxdaili.com/dailiip/1/{i+1}.html'
        response = requests.get(url=url)
        pattern = r'<td>([\d.]+)</td>'#匹配出<td></td>之间的内容，并只保留其中是数字和小数点.的部分
        results = re.findall(pattern, response.text)
        print(results)
        for n in range(10):
            try:
                ip_temp = results[2*n] + ":" + results[2*n+1]
                #print(ip_temp)
                ip.append(ip_temp)
            except Exception:
                print("没了")
                break
        #xpath报错，奇葩，浏览器调试正常
        #ip = result.xpath(f"/html/body/div[2]/div[2]/div[2]/div[2]/div[1]/div[2]/table/tbody/tr[{n + 1}]/td[1]/text()")[0]
        #port = result.xpath(f"/html/body/div[2]/div[2]/div[2]/div[2]/div[1]/div[2]/table/tbody/tr[{n + 1}]/td[2]/text()")[0]

        time.sleep(5)
    print(ip)